import numpy as np
import random

def Backtracking(func,dfunc,alpha,direc,x,t,c):
    """
    Backtracking回溯算法
    param:{
        func：函数
        dfunc：函数偏导数
        c：条件参数
        alpha：上一步步长
        direc：搜索方向
        x：当前点位置
        t：步长系数
    }
    return:newAlpha：下一步搜索步长
    """
    flag = True
    value = func(x)
    gradient = dfunc(x)
    factor = np.dot(gradient,direc)
    newAlpha = alpha
    
    

    while(flag):
        newValue = func(x+newAlpha*direc)
        
        if(newValue -value<= c*newAlpha*factor):
            flag = False
        else:
            newAlpha = newAlpha * t
        
    
    return newAlpha

